/*
 * ClientWindow.java
 *
 * Created on 10. říjen 2008, 14:54
 */

package gui;

import interfaces.Core_Events;

/**
 *
 * @author  Groš
 */
public class ClientWindow extends javax.swing.JFrame{

    /** Creates new form ClientWindow */
    public ClientWindow(Core_Events event) {
        setSystemLook();
        initComponents();
        this.event = event;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        bChooseFile = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        tFilePath = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        tIP = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        tPort = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        tText = new javax.swing.JTextArea();
        bSend = new javax.swing.JButton();
        bStop = new javax.swing.JButton();
        tSent = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Client");

        bChooseFile.setText("Choose File");
        bChooseFile.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bChooseFileActionPerformed(evt);
            }
        });

        jLabel1.setText("Selected file:");

        tFilePath.setEditable(false);
        tFilePath.setText("file path");
        tFilePath.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tFilePathActionPerformed(evt);
            }
        });

        jLabel2.setText("IP:");

        tIP.setText("127.0.0.1");

        jLabel3.setText("Port:");

        tPort.setText("5005");

        tText.setColumns(20);
        tText.setEditable(false);
        tText.setFont(new java.awt.Font("Arial", 1, 10));
        tText.setRows(5);
        jScrollPane1.setViewportView(tText);

        bSend.setText("Send");
        bSend.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bSendActionPerformed(evt);
            }
        });

        bStop.setText("Stop");
        bStop.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bStopActionPerformed(evt);
            }
        });

        tSent.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        tSent.setText("Sent: 0 b");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 329, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(bSend, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(bChooseFile, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel1)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(bStop, javax.swing.GroupLayout.DEFAULT_SIZE, 164, Short.MAX_VALUE)
                            .addComponent(tFilePath, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 164, Short.MAX_VALUE)))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addComponent(jLabel3)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(tPort))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addGap(8, 8, 8)
                            .addComponent(jLabel2)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(tIP, javax.swing.GroupLayout.PREFERRED_SIZE, 179, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
            .addComponent(tSent, javax.swing.GroupLayout.DEFAULT_SIZE, 349, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(tIP, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(tPort, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(tFilePath, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(bChooseFile))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bSend)
                    .addComponent(bStop))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 257, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(tSent))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void bChooseFileActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bChooseFileActionPerformed
    event.core_message("CW:Open-FCW");
}//GEN-LAST:event_bChooseFileActionPerformed

private void tFilePathActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tFilePathActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_tFilePathActionPerformed

private void bStopActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bStopActionPerformed
    event.core_message("CW:Stop");
}//GEN-LAST:event_bStopActionPerformed

private void bSendActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bSendActionPerformed
    event.core_message("CW:Send");
}//GEN-LAST:event_bSendActionPerformed

private void setSystemLook() {
    try {
      javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
    } 
    catch(Exception e) {
      System.out.println("Error setting native look: " + e);
    }
    this.setVisible(true);
}

public void show_filePath(String path) {
    tFilePath.setText(path);
}

public String getIP(){
    return tIP.getText();
}

public String getPort(){
    return tPort.getText();
}

public void writeText(String text) {
    tText.append(text + "\n");
}

public void clearText() {
    java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                tText.setText("");
            }
        });
}

/** expect only number of bytes */
public void changeStateLine(String text) {
    final String ftext = text;
    java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                tSent.setText("Sent: " + ftext + " b");
            }
        });   
}

public void show_SendButton(boolean show) {
    bSend.setEnabled(show);
}

public void show_StopButton(boolean show) {
    bStop.setEnabled(show);
}

public void show_ChooseFileButton(boolean show) {
    bChooseFile.setEnabled(show);
}

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bChooseFile;
    private javax.swing.JButton bSend;
    private javax.swing.JButton bStop;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField tFilePath;
    private javax.swing.JTextField tIP;
    private javax.swing.JTextField tPort;
    private javax.swing.JLabel tSent;
    private javax.swing.JTextArea tText;
    // End of variables declaration//GEN-END:variables
    private Core_Events event;
}
